(function(){
/*
 * drunk wisdom navigator
 * jiu shi zhe me niu bi.
 */
var dwn = {
    ownurl : document.scripts[document.scripts.length-1].src,
    init : function(wrapSelecter){
        if(!wrapSelecter) return;
        dwn.wrap = $(wrapSelecter);
        var marTop = dwn.arget('top') || 2,
            tarHeight = dwn.arget('height') || 32;
        $(document.body).append('<style type="text/css">.drunk-wisdom-navigator{float:right;height:'+tarHeight+'px;padding:1px;background:#fff;border-radius:5px;' +
            'box-sizing:content-box;-webkit-box-sizing:content-box;margin:'+marTop+'px 0 0 10px;}.drunk-wisdom-navigator a{display:inline-block;*display:inline;*zoom:1;' +
            'height:'+tarHeight+'px;line-height:'+tarHeight+'px;padding:0 10px;color:#fff;background:#f87948;margin-left:1px;border-radius:5px;font-size:14px;font-weight:bold;' +
            'font-family:"微软雅黑";}.drunk-wisdom-navigator a:hover{text-decoration:none;background:#990000}.drunk-wisdom-navigator a.first{margin-left:0;}</style>');
        $.ajax({url:dwn.ownurl.replace(/^(http:\/\/[^\/]+)\/?.*$/, '$1') + '/index.php/api/syncsites', dataType:'jsonp', success:dwn.build});
    },
    build : function(data){
        if(!$.isPlainObject(data)) return null;
        var filter = dwn.arget('filter'), fucked,
            elem = $('<div class="drunk-wisdom-navigator"></div>').prependTo(dwn.wrap);
        for(var i in data){
            if(new RegExp('\\b'+i+'\\b').test(filter)) continue;
            $('<a class="'+(fucked || !(fucked = 1) ? '' : 'first')+'" target="_blank" href="'+data[i].url_base+'">'+data[i].name+'</a>').appendTo(elem);
        }
    },
    arget : function(key){
        var matches = dwn.ownurl.match(new RegExp('\\b'+key+'=(.+?)(?:&|$)'));
        return !!matches ? matches[1] : null;
    }
};

$(function(){
    var selecter = dwn.arget('dwn');
    if(selecter) dwn.init(selecter);
});
})();